/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package mf.mfrpc.parser;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.ObjectBuffer;
import com.esotericsoftware.kryo.Serializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import mf.mfrpc.serialize.FileDataSource;
import mf.mfrpc.serialize.FileDataSourceIterator;
import mf.mfrpc.serialize.Parser;
import mf.mfrpc.serialize.SerializationException;

/**
 * Kryo序列化。
 */
public class KryoParser implements Parser {

    //thread safe
    private static final Kryo kryo = new Kryo();

    static {
        kryo.setRegistrationOptional(true);

        //single file datasource
        kryo.register(FileDataSource.class, new Serializer() {

            @Override
            public void writeObjectData(ByteBuffer buffer, Object object) {
                if (object instanceof FileDataSource) {
                    FileDataSource fds = (FileDataSource) object;
                    //adapte the properties for no no-arguments constructor
                    Object[] props = new Object[3];
                    //name
                    props[0] = fds.getName();
                    //size
                    props[1] = fds.getSize();
                    //lastModified
                    props[2] = fds.getLastModified();
                    //serialize datasource list
                    kryo.writeObjectData(buffer, props);
                    //not serialize input stream
                } else {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            }

            @Override
            public <T> T readObjectData(ByteBuffer buffer, Class<T> type) {
                Object[] props = kryo.readObjectData(buffer, Object[].class);
                return (T) new FileDataSource((String) props[0], null, (Long) props[1], (Long) props[2]);
            }
        });

        //multi file datasource
        kryo.register(FileDataSourceIterator.class, new Serializer() {

            @Override
            public void writeObjectData(ByteBuffer buffer, Object object) {
                if (object instanceof FileDataSourceIterator) {
                    FileDataSourceIterator fileit = (FileDataSourceIterator) object;
                    //serialize datasource list
                    kryo.writeObjectData(buffer, fileit.getDataSources());
                } else {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            }

            @Override
            public <T> T readObjectData(ByteBuffer buffer, Class<T> type) {
                return (T) new FileDataSourceIterator(kryo.readObjectData(buffer, ArrayList.class));
            }
        });
    }

    @Override
    public void writeObject(OutputStream output, Object object) {
        ObjectBuffer buffer = new ObjectBuffer(kryo);
        try {
            buffer.writeClassAndObject(output, object);
            output.flush();
        } catch (IOException e) {
            throw new SerializationException(e);
        }
    }

    @Override
    public <T> T readObject(InputStream input) {
        return (T) new ObjectBuffer(kryo).readClassAndObject(input);
    }

    @Override
    public <T> T readObject(InputStream input, Class<T> type) {
        return (T) readObject(input);
    }
}
